package com.ddc.study.feature.settings;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.ddc.study.R;
import com.ddc.study.base.BaseActivity;
import com.ddc.study.base.BaseApplication;
import com.ddc.study.feature.feedback.FeedbackActivity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created on 2018/8/1.
 *
 * @author lshun
 * @version 1.0
 */
public class SettingsActivity extends BaseActivity {
    @BindView(R.id.tv_log_out)
    View mLogOutView;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_settings;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getSupportActionBar() != null) getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        //根据登陆情况判断是否隐藏【退出登录】选项
        if (!((BaseApplication) getApplication()).isLoggedIn()) {
            mLogOutView.setVisibility(View.GONE);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }

    @OnClick(R.id.tv_feedback)
    void clickFeedback() {
        startActivity(new Intent(this, FeedbackActivity.class));
    }

    @OnClick(R.id.tv_about_us)
    void clickAboutUs() {
        View v = View.inflate(this, R.layout.layout_about_us, null);
        TextView tvVersion = v.findViewById(R.id.tv_version);
        try {
            tvVersion.setText(getString(R.string.version_tip,
                    getPackageManager().getPackageInfo(getPackageName(), 0).versionName));
        } catch (Exception e) {
            e.printStackTrace();
        }
        new AlertDialog.Builder(this)
                .setView(v)
                .setPositiveButton(android.R.string.ok, null)
                .show();
    }

    @OnClick(R.id.tv_log_out)
    void clickLogOut() {
        ((BaseApplication) getApplication()).logOut();
        finish();
    }
}
